package com.loop.inClass;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetDemo {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("a");
        set.add("hi");
        set.add("a");
        set.add("Hi");
        System.out.print(set);
        System.out.println();

        /**
         * 遍历方法 toArray
         */
        Object[] objects = set.toArray();
        for (Object obj:objects) {
            System.out.print((String)obj+" ");
        }
        System.out.println();
        /**
         * 遍历方法 iterator
         */
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next()+" ");
        }
        System.out.println();
        /**
         * 遍历方法 foreach
         */
        for (String s:set) {
            System.out.print(s+" ");
        }
    }
}
